package com.justnow.offer;

/**
 * @author justnow
 * Created on 2020-09-12
 * Description
 */
public class Solution43 {
    /**
     * 方法一，自己的方法，使用了两个指针来实现
     * @param head
     * @return
     */
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode currentNode = head, pre = head.next;
        while (pre != null) {
            if (currentNode.val == pre.val) {
                currentNode.next = pre.next;
            } else {
                currentNode = pre;
            }
            pre = pre.next;
        }
        return head;
    }

    /**
     * 方法二，官方只用了一个指针就可以实现了
     * @param head
     * @return
     */
    public ListNode deleteDuplicates2(ListNode head) {
        ListNode current = head;
        while (current != null && current.next != null) {
            if (current.next.val == current.val) {
                current.next = current.next.next;
            } else {
                current = current.next;
            }
        }
        return head;
    }
}

class ListNode {
    int val;
    ListNode next;
    public ListNode(int val) {
        this.val = val;
    }
}



